import DateUtil from "../../engine/DateUtil";

export class RingModel {

    public id: number;
    public desc: string;
    public begin_time: number;
    public end_time: number;
    public interval: number; //单位秒
    public priority: number;
    public del: number;
    public lastPlayTime: number = 0
    public static modelList: Array<RingModel> = []

    public static initTest() {
        for (let i = 0; i < 5; i++) {
            let model: RingModel = new RingModel();
            model.id = i;
            model.begin_time = DateUtil.now()
            model.end_time = DateUtil.now() + 1000 * 60 * 60
            model.interval = 1;
            model.priority = i;
            model.desc = "恭喜<color=#00b0f0>我是路人甲" + i + "</color>在<color=#ff0000>红中麻将</color>游戏中胡<color=#ff0000>“四暗刻”</color>赢得<color=#ffc000>100000</color>金币"
            this.modelList[i] = model
        }
        this.modelList.sort((a: RingModel, b: RingModel) => {
            return b.priority - a.priority;
        })
    }

    public static getPlayRing() {
        let now: number = DateUtil.now();
        // Logger.log("getPlayRing==", this.modelList)
        for (let i = 0; i < this.modelList.length; i++) {
            let model: RingModel = this.modelList[i]
            if (now >= model.begin_time && now <= model.end_time) {
                if (now - model.lastPlayTime >= model.interval * 1000) {
                    return model;
                }
            }
        }
    }

    public static checkDeletePlay(model: RingModel) {
        let now: number = DateUtil.now()
        model.lastPlayTime = now;
        if (now >= model.end_time) { //时间到删除
            this.deletePlay(model.id);
        }
        else if (model.interval == 0) { //不是循环播放删除
            this.deletePlay(model.id);
        } else {

        }
        // Logger.log("checkDeletePlay==", model, this.modelList)
    }

    public static deletePlay(id: number) {
        for (let i = 0; i < this.modelList.length; i++) {
            if (this.modelList[i].id == id) {
                this.modelList.splice(i, 1)
                break;
            }
        }
    }

    private static getRingByid(gid: number) {
        for (let i = 0; i < this.modelList.length; i++) {
            let model: RingModel = this.modelList[i]
            if (model.id == gid) {
                return model;
            }
        }
    }

    public static addRing(gid: number, context: string,
        begin_time: number, end_time: number, interval: number, priority: number) {
        let model: RingModel = this.getRingByid(gid)
        if(!model){
            model = new RingModel()
            this.modelList.push(model)
        }
        model.id = gid;
        model.desc = context;
        model.begin_time = begin_time;
        model.end_time = end_time;
        model.interval = interval;
        model.priority = priority;
        if (model.interval == 0 && this.modelList.length >= 100) {
            return
        }
        this.modelList.sort((a: RingModel, b: RingModel) => {
            return b.priority - a.priority;
        })
        // Logger.log("PaoMaDeng  addRing===", this.modelList)
     
    }
}